package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.BatchQueryUserInfoTestDataReq;
import com.sg.dto.base.framework.req.BatchQueryUserinfoListReq;
import com.sg.dto.base.framework.req.QueryUserInfoListReq;
import com.sg.dto.base.framework.req.QueryUserinfoDetailReq;
import com.sg.entity.Userinfo;
import com.sg.service.base.framework.MUserinfoService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.UserinfoMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MUserinfoServiceImpl
        extends ServiceImpl<UserinfoMapper, Userinfo>
        implements MUserinfoService {

    @Autowired
    UserinfoMapper userinfoMapper;

    /**
     * 1-2-02查询个人资料列表[1178]
     * gen by moon at 10/23/2022, 2:25:35 PM
     */
    @Trace(operationName = "1-2-02查询个人资料列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Userinfo> queryUserInfoList(QueryUserInfoListReq reqDto) {

        try {
            QueryWrapper<Userinfo> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Userinfo.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Userinfo.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Userinfo.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<Userinfo>) super.list(queryWrapper)).stream().map(item -> {
                item.setUserFace(CommonFunctionHelper.getFilePath(item.getUserFace()));//otherModel: ALL_PATH
                return item;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-02查询个人资料详情[155]
     * gen by moon at 3/31/2023, 3:24:00 AM
     */
    @Trace(operationName = "1-2-02查询个人资料详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Userinfo queryUserinfoDetail(QueryUserinfoDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<Userinfo> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<Userinfo> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                Userinfo retData = retList.get(0);
                retData.setUserFace(CommonFunctionHelper.getFilePath(retData.getUserFace()));//otherModel: ALL_PATH
                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-02批量查询个人资料列表[52]
     * gen by moon at 10/23/2022, 2:25:33 PM
     */
    @Trace(operationName = "1-2-02批量查询个人资料列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Userinfo> batchQueryUserinfoList(BatchQueryUserinfoListReq reqDto) {

        try {
            QueryWrapper<Userinfo> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getUserList() == null || reqDto.getUserList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("user_id", reqDto.getUserList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Userinfo.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Userinfo.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Userinfo.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<Userinfo>) super.list(queryWrapper)).stream().map(item -> {
                item.setUserFace(CommonFunctionHelper.getFilePath(item.getUserFace()));//otherModel: ALL_PATH
                return item;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-02新增个人资料[49]
     * gen by moon at 10/23/2022, 2:25:32 PM
     */
    @Trace(operationName = "1-2-02新增个人资料")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addUserinfo(Userinfo userinfo) {
        userinfo.setPersonalDataId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        userinfo.setIsValid("FALSE");
        userinfo.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        userinfo.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (super.save(userinfo)) {
            return userinfo.getPersonalDataId();
        } else {
            return "-1";
        }

    }

    /**
     * 1-2-02删除个人资料[51]
     * gen by moon at 10/23/2022, 2:25:33 PM
     */
    @Trace(operationName = "1-2-02删除个人资料")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteUserinfo(String personalDataId) {

        return super.removeById(personalDataId);
    }

    /**
     * 1-2-02修改个人资料[50]
     * gen by moon at 6/4/2023, 1:09:50 PM
     */
    @Trace(operationName = "1-2-02修改个人资料")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateUserInfo(Userinfo userinfo) {

        return super.updateById(userinfo);
    }

    /**
     * M1批量删个人资料测试[4857]
     * gen by moon at 10/23/2022, 2:25:35 PM
     */
    @Trace(operationName = "M1批量删个人资料测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteUserInfoTestData(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * M1批量查询个人资料测试[4858]
     * gen by moon at 10/23/2022, 2:25:36 PM
     */
    @Trace(operationName = "M1批量查询个人资料测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Userinfo> batchQueryUserInfoTestData(BatchQueryUserInfoTestDataReq reqDto) {

        try {
            QueryWrapper<Userinfo> queryWrapper = new QueryWrapper<>();

            if (reqDto.getUserInfoTestDataList() == null || reqDto.getUserInfoTestDataList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("user_id", reqDto.getUserInfoTestDataList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Userinfo.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Userinfo.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Userinfo.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<Userinfo>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
